package com.xingkeduo.service.impl;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.configuration.websocket.WebsocketMessageResponse;
import com.xingkeduo.dao.workpad.TStaffStockMapper;
import com.xingkeduo.dao.workpad.TStoreWorkPlanMapper;
import com.xingkeduo.model.bo.StockStatusEnum;
import com.xingkeduo.model.dto.*;
import com.xingkeduo.model.param.BatchLockStockParam;
import com.xingkeduo.model.param.StockParam;
import com.xingkeduo.model.po.TStaffStock;
import com.xingkeduo.model.po.TStaffStockExtend;
import com.xingkeduo.model.po.TStoreWorkPlan;
import com.xingkeduo.model.vo.*;
import com.xingkeduo.send.RabbitMqSender;
import com.xingkeduo.service.*;
import com.xingkeduo.service.StaffAttendanceService;
import com.xingkeduo.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.swing.Timer;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.xingkeduo.configuration.websocket.WebsocketMessageResponse.MessageTypeEnum.WORKPAD;

/**
 * @Author: 中文输入法发挥不稳定的刘川
 * @Date: 19-3-20 下午9:11
 */
@Service
public class StaffStockServiceImpl implements StaffStockService {

    private static Logger log = LoggerFactory.getLogger(StaffStockServiceImpl.class);


    @Autowired
    TStaffStockMapper staffStockMapper;
    @Autowired
    ShopService shopInfoService;
    @Autowired
    StaffAttendanceService attendanceService;
    @Autowired
    TStoreWorkPlanMapper storeWorkPlanMapper;

    /** 后台系统 **/
    @Autowired
    OperationService operationService;

    //服务间调用
    @Autowired
    OtherService otherService;

    //中间件
    @Autowired
    private RabbitMqSender rabbitMqSender;

    @Override
    public int batchInsertStaffStock(List<TStaffStock> staffStockList) {
        return staffStockMapper.batchInsertStaffStock(staffStockList);
    }

    @Override
    public Integer getStaffNumInShop(Date date, Long shopId) {
        return staffStockMapper.staffNumInShop(date, shopId);
    }

    @Override
    public List<TStaffStock> getEnableStockListByDateAndStoreId(Date date, Long shopId) {
        return staffStockMapper.selectEnableStockByDateAndStoreId(date, shopId);
    }

    @Override
    public List<TStaffStock> getEnableStockListByDateAndStoreIdAndStaffId(Date date, Long shopId, Long staffId) {
        return staffStockMapper.selectEnableStockByDateAndStoreIdAndStaffId(date, shopId, staffId);
    }

    @Override
    public List<TStaffStock> getIdAndStaffIdByDateAndStoreId(Date date, Long shopId) {
        return staffStockMapper.selectIdAndStaffIdByDateAndStoreId(date, shopId);
    }

    @Override
    public int updateBatchStockToSystemOccupyByIdList(List<Long> idList) {
        if (null == idList || idList.isEmpty()) {
            return 0;
        }
        return staffStockMapper.updateBatchStockToSystemOccupyByIdList(idList);
    }

    /**
     * @description 平移库存方法
     * @Author 情绪极其不稳定的Shadow
     * @Param [sourceStockList] 原库存
     * @Param [targetStockList] 目标库存
     * @return java.util.List<com.xingkeduo.model.po.TStaffStock>
     **/
    @Override
    public List<TStaffStock> getNewStockListAfterMove(List<TStaffStock> sourceStockList, List<TStaffStock> targetStockList) {
        //被平移库存为空 则直接返回目标库存列表
        if (null == sourceStockList || sourceStockList.isEmpty()) {
            return targetStockList;
        }
//        List<TStaffStock> needToDoList = getAlreadyUsedStock(sourceStockList);
        //如果没有需要平移的库存 则直接返回目标库存列表
//        if (needToDoList.isEmpty()) {
//            return targetStockList;
//        }
        for (TStaffStock sourceStock : sourceStockList) {
            //此处添加需要转移的库存状态（当前平移代付款，带服务状态库存）
            if (sourceStock.getStatus().equals(StockStatusEnum.WAIT_FOR_PAYMENT.getValue())
                    || sourceStock.getStatus().equals(StockStatusEnum.WAIT_FOR_SERVICE.getValue())
                    || sourceStock.getStatus().equals(StockStatusEnum.IN_SERVICE.getValue())) {
                Integer targetStockNo = 0;
                //TODO 此行代码是原有逻辑 随机平移逻辑
//                long minInterval = abs(sourceStock.getStartTime().getTime() - targetStockList.get(targetStockNo).getStartTime().getTime());
                int i = 0;
                for (TStaffStock targetStock : targetStockList) {
                        //TODO 此if判断代码是原有逻辑 if中修改内容 把原有的判断条件修改
//                           !targetStock.getStatus().equals(StockStatusEnum.WAIT_FOR_PAYMENT.getValue())
//                        && !targetStock.getStatus().equals(StockStatusEnum.WAIT_FOR_SERVICE.getValue())
//                        && !targetStock.getStatus().equals(StockStatusEnum.IN_SERVICE.getValue())
                    if (!targetStock.getStatus().equals(StockStatusEnum.SYSTEM_OCCUPY.getValue())) {
                        //TODO 此行代码是原有逻辑 随机平移逻辑
//                        long interval = abs(sourceStock.getStartTime().getTime() - targetStock.getStartTime().getTime());
                        log.info("历史库存时间sourceStock.getStartTime()--->"+sourceStock.getStartTime());
                        log.info("新生成库存时间targetStockList.get(i).getStartTime()--->"+targetStockList.get(i).getStartTime());
                        log.info("历史库存时间-新生成库存时间=--->"+String.valueOf(sourceStock.getStartTime().getTime() - targetStockList.get(i).getStartTime().getTime()));
                        //TODO 此if判断代码是原有逻辑 随机平移逻辑  2019-07-24 修改成 只平移时间相当的库存
//                        if (interval > minInterval) {
//                            minInterval = interval;
//                            targetStockNo = i;
//                        }
                        if(sourceStock.getStartTime().getTime() - targetStockList.get(i).getStartTime().getTime() == 0){
                            log.info("需要平移到第i"+i+"个库存上");
                            log.info("被平移的库存数据sourceStock.getStatus()--->>>"+sourceStock.getStatus());
                            log.info("被平移的库存数据sourceStock.getOrderId()--->>>"+sourceStock.getOrderId());
                            log.info("被平移的库存数据sourceStock.getCustomerId()--->>>"+sourceStock.getCustomerId());
                            log.info("被平移的库存数据sourceStock.getLineNo()--->>>"+sourceStock.getLineNo());
                            log.info("被平移的库存数据sourceStock.getUserPath()--->>>"+sourceStock.getUserPath());
                            targetStockList.get(i).setStatus(sourceStock.getStatus());
                            targetStockList.get(i).setOrderId(sourceStock.getOrderId());
                            targetStockList.get(i).setCustomerId(sourceStock.getCustomerId());
                            targetStockList.get(i).setLineNo(sourceStock.getLineNo());
                            targetStockList.get(i).setUserPath(sourceStock.getUserPath());
                        }
                    }
                    i++;
                }
                //TODO 此处代码 此行代码是原有逻辑 随机平移逻辑
//                log.info("需要平移到第i"+i+"个库存上");
//                log.info("被平移的库存数据sourceStock.getStatus()--->>>"+sourceStock.getStatus());
//                log.info("被平移的库存数据sourceStock.getOrderId()--->>>"+sourceStock.getOrderId());
//                log.info("被平移的库存数据sourceStock.getCustomerId()--->>>"+sourceStock.getCustomerId());
//                log.info("被平移的库存数据sourceStock.getLineNo()--->>>"+sourceStock.getLineNo());
//                log.info("被平移的库存数据sourceStock.getUserPath()--->>>"+sourceStock.getUserPath());
//                targetStockList.get(targetStockNo).setStatus(sourceStock.getStatus());
//                targetStockList.get(targetStockNo).setOrderId(sourceStock.getOrderId());
//                targetStockList.get(targetStockNo).setCustomerId(sourceStock.getCustomerId());
//                targetStockList.get(targetStockNo).setLineNo(sourceStock.getLineNo());
//                targetStockList.get(targetStockNo).setUserPath(sourceStock.getUserPath());
            }
        }
        return targetStockList;
    }

    @Override
    public int deleteStockByIdList(List<Long> idList) {
        return staffStockMapper.deleteStockByIdList(idList);
    }

    @Override
    public int deleteStockByDateAndStoreId(Date date, Long storeId) {
        return staffStockMapper.deleteStockByDateAndStoreId(date,storeId);
    }

    @Override
    public int deleteStockByAttendanceIdList(List<Long> attendanceIdList) {
        return staffStockMapper.deleteStockByAttendanceIdList(attendanceIdList);
    }

    @Override
    public int deleteStockByDateAndstaffId(Date date, Long statffId) {
        return staffStockMapper.deleteStockByDateAndStaffId(date, statffId);
    }

    @Override
    public int deleteStockByDateAndstaffIdAndStoreId(Date date, Long statffId, Long storeId) {
        return staffStockMapper.deleteStockByDateAndstaffIdAndStoreId(date, statffId, storeId);
    }

    @Override
    public int unableStockByStoreIdAndStaffIdAndWeek(Long storeId, Long staffId, List<Date> dateList) {
        List<TStaffStock> beforeUnableStaffStockList = staffStockMapper.selectStockByStoreIDAndStaffIdAndDate(storeId, staffId, dateList);
        //TODO 换成接口 2019-04-25 以替换成接口  待优化
        ShopInfoVo shopInfoVo = otherService.getShopInfoByShopId(storeId);
        if(null == shopInfoVo){
            return 0;
        }
        //获取最长服务时间  注意接口返回的是分 后面处理的时候是秒
        Integer intervalTime = shopInfoVo.getParticle() * 60;
        //获取店铺座位数
        Integer seatNum = shopInfoVo.getSeatNum();
        if(intervalTime == null || seatNum == null){
            return 0;
        }
//        Integer seatNum = shopInfoService.getSeatNumByShopId(storeId);
//        Integer intervalTime = shopInfoService.getMaxTimeByShopId(storeId);
        int involveRows=0;
        for (Date date : dateList) {
            Integer alreadyStaffNumInStoreTotal = getStaffNumInShop(date, storeId);
            //如果删除之前 存在自动休息逻辑 则将之前的库存系统占用状态 干掉 重新自动休息
            if (alreadyStaffNumInStoreTotal > seatNum) {
                int unOccupyRows = staffStockMapper.turnBackToFreeByStoreAndDate(storeId, date);
                int unableRows = staffStockMapper.unableStockByStoreIdAndStaffIdAndDay(storeId, staffId, date);
                if (unOccupyRows > 0) {
                    if (unableRows > 0) {
                        //自动重新休息
                        attendanceService.makeRestStatusBySystem(storeId,date,intervalTime,alreadyStaffNumInStoreTotal,seatNum);
                    }
                }
            }else {
                 staffStockMapper.unableStockByStoreIdAndStaffIdAndDay(storeId, staffId, date);
            }
        }
        return involveRows;
    }

    @Override
    public int updateStockStatusByStoreIdAndDate(Date date, Long storeId) {
        return staffStockMapper.updateStockStatusByStoreIdAndDate(date, storeId);
    }

    @Override
    public int updateServerTimeAndBusinessTime(String shopId, String serverTime, String businessTime, String modifier) {
        TStoreWorkPlan tStoreWorkPlan = storeWorkPlanMapper.selectStorePlanByStoreId(Long.parseLong(shopId));
        if(null == tStoreWorkPlan){
            return -1;
        }
        //1.根据nowDate删除shopId当天之后的所有库存
        Date nowDate = new Date();
        int deleteStockInt = staffStockMapper.deleteStockByAfterDateAndStoreId(nowDate, Long.parseLong(shopId), Long.parseLong(modifier));
        //2.清空店铺班次设置
        int emptySchedulingInt = storeWorkPlanMapper.updateEmptySchedulingByShopId(Long.parseLong(shopId), Long.parseLong(modifier));
        //3.把第二天及以后的排班时间置空
        int emptyAttendance = attendanceService.updateEmptyAttendanceTimeByAfterDateAndShopId(nowDate, Long.parseLong(shopId), Long.parseLong(modifier));
        //4.把当周及以后的所有早晚全班全部置为临时状态
        int resetInt = attendanceService.updateResetAttendanceTypeByAfterDateAndShopId(TimeUtil.getDateOfMonday(new Date(), 0), Long.parseLong(shopId), Long.parseLong(modifier));
        if(deleteStockInt > 0 || emptySchedulingInt > 0 || resetInt > 0 || emptyAttendance > 0){
            rabbitMqSender.sendMessage("websocket_transfer_exchange", "websocket_transfer_key", JSON.toJSONString(new WebsocketMessageResponse<>(WORKPAD, String.valueOf(shopId), null)));
        }
        return 1;
    }

    @Override
    public int lockStockBystockId(Long stockId, Long orderNo, byte status, String lineNo, String userPath, Long storeId) {
        log.info("锁定库存参数：stockId--->>>"+stockId);
        log.info("锁定库存参数：orderNo--->>>"+orderNo);
        log.info("锁定库存参数：status--->>>"+status);
        log.info("锁定库存参数：lineNo--->>>"+lineNo);
        log.info("锁定库存参数：userPath--->>>"+userPath);
        log.info("锁定库存参数：storeId--->>>"+storeId);
        TStaffStock staffStock = null;
        if(!StringUtils.isEmpty(stockId)){
            staffStock = staffStockMapper.selectByPrimaryKey(stockId);
            if(staffStock.getStatus() != 0){
                return -3;
            }
        }else {
            staffStock = staffStockMapper.selectByStoreIdAndOrderNo(storeId, orderNo);
        }
        log.info("锁定库存查询库存staffStock--->>>"+staffStock);
        if(null == staffStock){
            return -1;
        }
        log.info("锁定库存查询库存staffStock.getOrderId()--->>>"+staffStock.getOrderId());
        log.info("锁定库存查询库存!staffStock.getOrderId().equals(orderNo)--->>>"+!orderNo.equals(staffStock.getOrderId()));
        if(!StringUtils.isEmpty(staffStock.getOrderId()) && !orderNo.equals(staffStock.getOrderId())){
            return -2;
        }
        int i = 0;
        if(!StringUtils.isEmpty(stockId)){
            i = staffStockMapper.updateStockBystockId(stockId, orderNo, status, lineNo, userPath);
        }else {
            i = staffStockMapper.updateStockByStoreIdAndOrderId(storeId, orderNo, status, lineNo, userPath);
        }
        log.info("锁定库存查询库存操作---i--->>>"+i);
        rabbitMqSender.sendMessage("websocket_transfer_exchange", "websocket_transfer_key", JSON.toJSONString(new WebsocketMessageResponse<>(WORKPAD, String.valueOf(staffStock.getStoreId()), null)));
        return i;
    }

    @Override
    public int batchLockStockBystockId(List<BatchLockStockParam> batchLockStockParams) {
        for(BatchLockStockParam batchLockStockParam : batchLockStockParams){
            log.info("锁定库存参数：stockId--->>>"+batchLockStockParam.getStockId());
            log.info("锁定库存参数：orderNo--->>>"+batchLockStockParam.getOrderNo());
            log.info("锁定库存参数：status--->>>"+batchLockStockParam.getStatus());
            log.info("锁定库存参数：lineNo--->>>"+batchLockStockParam.getLineNo());
            log.info("锁定库存参数：userPath--->>>"+batchLockStockParam.getUserPath());
            log.info("锁定库存参数：storeId--->>>"+batchLockStockParam.getStoreId());
        }
        BatchLockStockParam stockParam = batchLockStockParams.get(0);
        TStaffStock staffStock = null;
        if(!StringUtils.isEmpty(stockParam.getStockId())){
            staffStock = staffStockMapper.selectByPrimaryKey(stockParam.getStockId());
            if(staffStock.getStatus() != 0){
                return -3;
            }
        }else {
            log.info("库存变更参数：stockParam.getStockId()--->>>"+stockParam.getStoreId());
            log.info("库存变更参数：stockParam.getOrderNo()--->>>"+stockParam.getOrderNo());
            staffStock = staffStockMapper.selectByStoreIdAndOrderNo(stockParam.getStoreId(), stockParam.getOrderNo());
        }
        log.info("锁定库存查询库存staffStock--->>>"+staffStock);
        if(null == staffStock){
            return -1;
        }

        log.info("锁定库存查询库存staffStock.getOrderId()--->>>"+staffStock.getOrderId());
        log.info("锁定库存查询库存!staffStock.getOrderId().equals(orderNo)--->>>"+!stockParam.getOrderNo().equals(staffStock.getOrderId()));
        if(!StringUtils.isEmpty(staffStock.getOrderId()) && !stockParam.getOrderNo().equals(staffStock.getOrderId())){
            return -2;
        }
        int i = 0;
        if(!StringUtils.isEmpty(stockParam.getStockId())){
            i = staffStockMapper.updateBatchStockBystockId(batchLockStockParams);
        }else {
            //TODO 目前Babar锁定双库存，用的还是锁定库存的mapper ∵锁定库存的时候是根据店铺IDand订单号操作，由于Babar的双库存订单号and店铺Id肯定一样
            i = staffStockMapper.updateStockByStoreIdAndOrderId(stockParam.getStoreId(), stockParam.getOrderNo(), stockParam.getStatus(), stockParam.getLineNo(), stockParam.getUserPath());
        }
        log.info("锁定库存查询库存操作---i--->>>"+i);
        rabbitMqSender.sendMessage("websocket_transfer_exchange", "websocket_transfer_key", JSON.toJSONString(new WebsocketMessageResponse<>(WORKPAD, String.valueOf(staffStock.getStoreId()), null)));
        return i;
    }

    @Override
    public int unlockStockBystockId(Long storeId, Long orderNo) {
        TStaffStock staffStock = staffStockMapper.selectByStoreIdAndOrderNo(storeId, orderNo);
        if( null == staffStock){
            return -1;
        }
        int i = staffStockMapper.updateUnlockStockBystockId(storeId, orderNo);
        rabbitMqSender.sendMessage("websocket_transfer_exchange", "websocket_transfer_key", JSON.toJSONString(new WebsocketMessageResponse<>(WORKPAD, String.valueOf(staffStock.getStoreId()), null)));
        return i;
    }

    @Override
    public int batchUnlockStockBystockId(List<BatchLockStockParam> batchLockStockParams) {
        //TODO 目前Babar释放双库存，用的还是释放库存的mapper ∵释放库存的时候是根据店铺IDand订单号操作，由于Babar的双库存订单号and店铺Id肯定一样
        BatchLockStockParam batchLockStockParam = batchLockStockParams.get(0);
        TStaffStock staffStock = staffStockMapper.selectByStoreIdAndOrderNo(batchLockStockParam.getStoreId(), batchLockStockParam.getOrderNo());
        if( null == staffStock){
            return -1;
        }
        int i = staffStockMapper.updateUnlockStockBystockId(batchLockStockParam.getStoreId(), batchLockStockParam.getOrderNo());
        rabbitMqSender.sendMessage("websocket_transfer_exchange", "websocket_transfer_key", JSON.toJSONString(new WebsocketMessageResponse<>(WORKPAD, String.valueOf(staffStock.getStoreId()), null)));
        return i;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public int changeStockBystockIdAndorderNo(Long storeId, Long orderNo, Long stockId, Byte status, String lineNo) {
        log.info("改约,解锁&锁定库存接口参数：storeId--->>>"+storeId);
        log.info("改约,解锁&锁定库存接口参数：orderNo--->>>"+orderNo);
        log.info("改约,解锁&锁定库存接口参数：stockId--->>>"+stockId);
        //释放旧库存 start 准备数据
        TStaffStock unlockStaffStock = staffStockMapper.selectByStoreIdAndOrderNo(storeId, orderNo);
        if(null == unlockStaffStock){
            return -1;//释放库存不存在
        }
        TStaffStock lockStaffStock = null;
        if(!StringUtils.isEmpty(stockId)){
            lockStaffStock = staffStockMapper.selectByPrimaryKey(stockId);
            if(lockStaffStock.getStatus() != 0){
                return -3;//改约库存,锁定新库存时,库存状态不是空闲
            }
        }
        log.info("改约,锁定库存staffStock--->>>"+lockStaffStock);
        if(null == lockStaffStock){
            return -4;//查询不到改约库存
        }
        log.info("改约,锁定库存查询库存unlockStaffStock.getOrderId()--->>>"+unlockStaffStock.getOrderId());
        log.info("改约,锁定库存查询库存!unlockStaffStock.getOrderId().equals(orderNo)--->>>"+!orderNo.equals(unlockStaffStock.getOrderId()));
        if(!StringUtils.isEmpty(unlockStaffStock.getOrderId()) && !orderNo.equals(unlockStaffStock.getOrderId())){
            return -2;//并发下单问题,
        }
        //更新老库存-解约操作
        int i = staffStockMapper.updateUnlockStockBystockId(storeId, orderNo);
        log.info("释放库存查询库存操作---i--->>>"+i);
        if(i != 1){
        	log.error("【改约失败】老库存更新条数不唯一,storeId;{},orderNo:{}",storeId,orderNo);
        	throw new RuntimeException("改约失败-->更新条数不唯一");
        }
        //更新新库存-锁定操作
    	int j = staffStockMapper.updateStockBystockId(stockId, orderNo, status, lineNo, unlockStaffStock.getUserPath());
    	log.info("锁定库存查询库存操作---j--->>>"+j);
    	if(j != 1){
        	log.error("【改约失败】新库存更新条数不唯一,storeId;{},orderNo:{}",storeId,orderNo);
        	throw new RuntimeException("改约失败-->更新条数不唯一");
        }
        //释放旧库存 end
        rabbitMqSender.sendMessage("websocket_transfer_exchange", "websocket_transfer_key", JSON.toJSONString(new WebsocketMessageResponse<>(WORKPAD, String.valueOf(storeId), null)));
        return 1;
    }

    @Override
    public int updateStockStatusRestOrOccupy(Long id, byte status, Long managerId) {
        TStaffStock staffStock = staffStockMapper.selectByPrimaryKey(id);
        if (StockStatusEnum.SYSTEM_OCCUPY.getValue() == status ||StockStatusEnum.MANUAL_REST.getValue() == status) {
            if(null==staffStock){
                return -3;
            }
            int alreadyInNum = staffStockMapper.selectNumOfInUseByStoreAndDateAndTime(staffStock.getStoreId(), staffStock.getDate(), staffStock.getStartTime());
            ShopInfoVo shopInfoVo = otherService.getShopInfoByShopId(staffStock.getStoreId());
            if(null==shopInfoVo){
                return -5;
            }
            Integer seatNum = shopInfoVo.getSeatNum();
            if (seatNum > alreadyInNum) {
                rabbitMqSender.sendMessage("websocket_transfer_exchange", "websocket_transfer_key", JSON.toJSONString(new WebsocketMessageResponse<>(WORKPAD, String.valueOf(staffStock.getStoreId()), null)));
                return staffStockMapper.updateStockOccupyToRestById(staffStock.getId(),managerId);
            }else {
                return -2;
            }
        }
        if (StockStatusEnum.FREE.getValue() == status) {
            rabbitMqSender.sendMessage("websocket_transfer_exchange", "websocket_transfer_key", JSON.toJSONString(new WebsocketMessageResponse<>(WORKPAD, String.valueOf(staffStock.getStoreId()), null)));
            return staffStockMapper.updateStockRestToOccupyById(id,managerId);
        }
        return -1;
    }

    @Override
    public StockSmallProgramVo getStockIdByStaffIdAndStoreId(Long staffId, Long storeId, String startTime, String selectDate) {
        TStaffStock staffStock = staffStockMapper.selectStockIdByStaffIdAndStoreId(staffId, storeId, startTime, selectDate);
        if(staffStock == null){
            return null;
        }
        String date = TimeUtil.DateToFormat(staffStock.getDate(), "yyyy-MM-dd");
        String staTime = TimeUtil.DateToFormat(staffStock.getStartTime(), "HH:mm:ss");
        String endTime = TimeUtil.DateToFormat(staffStock.getEndTime(), "HH:mm:ss");
        StockSmallProgramVo stockSmallProgramVo = new StockSmallProgramVo();
        stockSmallProgramVo.setId(staffStock.getId());
        stockSmallProgramVo.setStartTime(date+" "+staTime);
        stockSmallProgramVo.setEndTime(date+" "+endTime);
        return stockSmallProgramVo;
    }

    @Override
    public List<StockSmallProgramVo> getStockIdByStaffIdAndBaberStoreId(Long staffId, Long storeId, String startTime, String selectDate) {
        List<TStaffStock> staffStocks = staffStockMapper.selectStockIdByStaffIdAndBaberStoreId(staffId, storeId, startTime, selectDate);
        if(CollectionUtils.isEmpty(staffStocks)){
            return null;
        }
        List<StockSmallProgramVo> stockSmallProgramVos = new ArrayList<>();
        for(TStaffStock staffStock : staffStocks){
            if(staffStock.getStatus() != 0){
                return null;
            }
        }
        for(TStaffStock staffStock : staffStocks){
            String date = TimeUtil.DateToFormat(staffStock.getDate(), "yyyy-MM-dd");
            String staTime = TimeUtil.DateToFormat(staffStock.getStartTime(), "HH:mm:ss");
            String endTime = TimeUtil.DateToFormat(staffStock.getEndTime(), "HH:mm:ss");
            StockSmallProgramVo stockSmallProgramVo = new StockSmallProgramVo();
            stockSmallProgramVo.setId(staffStock.getId());
            stockSmallProgramVo.setStartTime(date+" "+staTime);
            stockSmallProgramVo.setEndTime(date+" "+endTime);
            stockSmallProgramVos.add(stockSmallProgramVo);
        }
        return stockSmallProgramVos;
    }

    @Override
    public List<StockWorkTimeVo> getMinFreeStockByStoreId(Long storeId, String selectDate) {
        //根据店铺Id 查询当天所有 存在库存的发型师Id 集合
        List<Long> staffIds = staffStockMapper.selectStockStaffIdByDateAndStoreId(storeId, TimeUtil.DateToFormat(new Date(), "yyyy-MM-dd"));
        List<TStaffStock> tStaffStocks = staffStockMapper.selectMinFreeStockByStoreId(storeId, null, TimeUtil.DateToFormat(new Date(), "yyyy-MM-dd"));
        List<StockWorkTimeVo> vos = new ArrayList<>();
        //1.拼接工作时间
        List<StockWorkTimeVo> workTimeVos = filterStockByWorkTime(tStaffStocks);
        //2.根据staffId分组
        Map<Long, List<StockWorkTimeVo>> workTimeCollect = workTimeVos.stream().collect(Collectors.groupingBy(StockWorkTimeVo::getStaffId));
        //3.算出map每个list中时间最小的元素
        for (Map.Entry<Long, List<StockWorkTimeVo>> entry : workTimeCollect.entrySet()) {
            List<StockWorkTimeVo> timeVos = workTimeCollect.get(entry.getKey());
            Collections.sort(timeVos, Comparator.comparing(StockWorkTimeVo::getWorkTime));
            vos.add(timeVos.get(0));
        }
        //4.计算
        //"today_rest":false,   //发型师今日是否休息
        //"today_full":false,   //发型师今日是否已被约满
        //"nearly_appointment_timestamp":1554275100,   //最早可约的时间戳
        //"nearly_appointment_time":"15:05",   //最早可约的时刻
        //"nearly_appointment_date":"今天" or "2019-04-06",   //最早可约的日期
        //"nearly_appointment_date_format":"2019-04-03",   //最早能够预约的日期（格式化）
        for(StockWorkTimeVo vo : vos){
            System.out.println(vo.getStaffId());
            String workTime = vo.getWorkTime();
            String[] dateAndTime = workTime.split(" ");
            String date = dateAndTime[0];
            String time = dateAndTime[1].substring(0,5);
            String nowDate = TimeUtil.DateToFormat(new Date(), "yyyy-MM-dd");
            //TODO 2019-05-21 未处理 不休息 库存预约满的情况  因为sql 查询出来的库存 条件中带有 status = 0 空闲状态
            //TODO 2019-05-21 已处理 但是感觉耦合有点高 后期有时间调整下 目前实现方案是先查出店铺下所有有库存的发型师Id 在遍历的时候对比
            //TODO 先对比日期是否在同一天 如果是再对比当前的ID 是否在 List<Long> staffIds 集合中
            //当天有排班  setTodayRest = false   有库存 setTodayFull = false
            if(date.equals(nowDate)){
                vo.setTodayRest(false);
                vo.setTodayFull(false);
                vo.setNearlyAppointmentDateFormat(date);
                vo.setNearlyAppointmentDate(TimeUtil.isToday(date,"yyyy-MM-dd"));
                vo.setNearlyAppointmentTime(time);
                vo.setNearlyAppointmentTimestamp(TimeUtil.DateToTimestamp(workTime));
            }else{
                //当天没库存判断 发型师Id是否存在staffIds中
                if(staffIds.contains(vo.getStaffId())){
                    //存在setTodayRest = false  被约满 setTodayFull = true
                    vo.setTodayRest(false);
                    vo.setTodayFull(true);
                    vo.setNearlyAppointmentDateFormat(date);
                    vo.setNearlyAppointmentDate(TimeUtil.isToday(date,"yyyy-MM-dd"));
                    vo.setNearlyAppointmentTime(time);
                    vo.setNearlyAppointmentTimestamp(TimeUtil.DateToTimestamp(workTime));
                }else{
                    //不存在 setTodayRest = true  被约满 setTodayFull = false
                    vo.setTodayRest(true);
                    vo.setTodayFull(false);
                    vo.setNearlyAppointmentDateFormat(date);
                    vo.setNearlyAppointmentDate(TimeUtil.isToday(date,"yyyy-MM-dd"));
                    vo.setNearlyAppointmentTime(time);
                    vo.setNearlyAppointmentTimestamp(TimeUtil.DateToTimestamp(workTime));
                }
            }
        }
        //1.增加当前时间之后没有空闲库存的发型师元素
        List<Long> staffStockIds = new ArrayList<>();
        for(StockWorkTimeVo stockWorkTimeVo : vos){
            staffStockIds.add(stockWorkTimeVo.getStaffId());
        }
        for(Long staffId : staffIds){
            if(!staffStockIds.contains(staffId)){
                StockWorkTimeVo demoStockWorkTimeVo = new StockWorkTimeVo();
                //TODO 因为猜测不可预约的状态展示 应该一定是今日不休息 切今日约满的状态 so 直接set的状态
                demoStockWorkTimeVo.setTodayRest(false);
                demoStockWorkTimeVo.setTodayFull(true);
                demoStockWorkTimeVo.setStaffId(staffId);
                demoStockWorkTimeVo.setNearlyAppointmentTimestamp(0);
                demoStockWorkTimeVo.setNearlyAppointmentTime("");
                demoStockWorkTimeVo.setNearlyAppointmentDate("");
                demoStockWorkTimeVo.setNearlyAppointmentDateFormat("");
                vos.add(demoStockWorkTimeVo);
            }
        }
        log.info("发型师最早可约时间排序前start");
        for(StockWorkTimeVo vo : vos){
            log.info("发型师ID："+vo.getStaffId());
            log.info("发型师最早可约时间date："+ vo.getNearlyAppointmentDateFormat()+" "+vo.getNearlyAppointmentTime());
            log.info("发型师最早可约时间："+vo.getNearlyAppointmentTimestamp());
        }
        log.info("发型师最早可约时间排序前end");
        //开始排序
//        vos.stream().sorted(Comparator.comparing(StockWorkTimeVo::getNearlyAppointmentTimestamp));
        Collections.sort(vos, Comparator.comparing(StockWorkTimeVo::getNearlyAppointmentTimestamp));
        List<StockWorkTimeVo> resultsVos = new ArrayList<>();
        List<StockWorkTimeVo> tempVos = new ArrayList<>();
        for(StockWorkTimeVo vo : vos){
            if(vo.getNearlyAppointmentTimestamp() == 0){
                tempVos.add(vo);
            }else {
                resultsVos.add(vo);
            }
        }
        if(!CollectionUtils.isEmpty(tempVos)){
            for(StockWorkTimeVo vo : tempVos){
                resultsVos.add(vo);
            }
        }
        log.info("发型师最早可约时间排序后start");
        for(StockWorkTimeVo vo : resultsVos){
            log.info("发型师ID："+vo.getStaffId());
            log.info("发型师最早可约时间date："+ vo.getNearlyAppointmentDateFormat()+" "+vo.getNearlyAppointmentTime());
            log.info("发型师最早可约时间："+vo.getNearlyAppointmentTimestamp());
        }
        log.info("发型师最早可约时间排序后end");
        return resultsVos;
    }

    /**
     * @description  1.根据指定时间查询当前店铺第二天及以后的所有带有订单的库存---->>>修改库存颗粒or营业时间
     *               2.根据指定时间查询当前店铺当天级以后的订单--->>>删除人员时查询会影响的订单
     * @Author 情绪极其不稳定的Shadow
     * @Param [nowDate, shopId]
     * @return java.util.List<java.lang.Long>
     **/
    @Override
    public List<Long> getStocksByDateAndStoreId(Long shopId, List<Long> staffIds, Date date) {
        log.info("人员集合为staffIds"+staffIds.toString());
        List<Long> orderIds = new ArrayList<>();
        if(!CollectionUtils.isEmpty(staffIds)){
            //用来删除人员时查询会影响的订单
            Date dateOfMonday = TimeUtil.getDateOfMonday(date, 0);
            Date dateOfSunday = TimeUtil.getDateOfSunday(dateOfMonday);
            String monday = TimeUtil.DateToFormat(dateOfMonday, "yyyy-MM-dd");
            String sunday = TimeUtil.DateToFormat(dateOfSunday, "yyyy-MM-dd");
            log.info("monday:"+monday);
            log.info("sunday:"+sunday);
            orderIds = staffStockMapper.selectStocksByDateAndStoreId(shopId, staffIds, monday, sunday);
        }else{
            //用来查询更改库存颗粒 or 营业时间时会影响的订单
            String selectDate = TimeUtil.getFetureDate(1, new Date());
            orderIds = staffStockMapper.selectStocksByDateAndStoreId(shopId, null, selectDate, "");
        }
        return orderIds;
    }

    /**
     * @description  根据店铺Id找到受影响的订单,请求订单系统拿到对应数据，组装返回.
     * @Author 情绪极其不稳定的Shadow
     * @Param [shopId]
     * @return java.util.List<com.xingkeduo.model.vo.OrderInfoVo>
     **/
    @Override
    public List<OrderInfoVo> stockOrder(Long shopId, List<Long> staffIds, Date selectDate) {
        //1.根据nowDate查出之后又订单的库存
        List<Long> orderIds = getStocksByDateAndStoreId(shopId, staffIds, selectDate);
        List<String> orderIdList = new ArrayList<>();
        orderIds.removeAll(Collections.singleton(null));
        for(Long orderId: orderIds){
            orderIdList.add(String.valueOf(orderId));
        }
        String [] orderNos = orderIdList.toArray(new String[orderIdList.size()]);
        //2.根据订单ID去订单系统查询订单详情
        List<OrderInfoVo> orderInfos = otherService.getOrderInfoByOrderNos(orderNos);
        if(CollectionUtils.isEmpty(orderInfos)){
            return new ArrayList<>();
        }
        //3.根据订单中的用户ID去用户系统查出用户电话  TODO 目前由订单系统直接返回的用户电话
//        for(OrderInfoVo orderInfoVo : orderInfos) {
//            if (StringUtils.isEmpty(orderInfoVo.getAppointmentStartTime()))
//                orderInfoVo.setAppointmentStartTime("");
//            if (StringUtils.isEmpty(orderInfoVo.getUserMobile()))
//                orderInfoVo.setUserMobile("");
//            if (StringUtils.isEmpty(orderInfoVo.getWaitingLineNo()))
//                orderInfoVo.setWaitingLineNo("");
//        }
        //4.组织数据返回
        log.info("受影响的订单--->>>"+orderInfos.toString());
        return orderInfos;
    }

    @Override
    public StockForOrderVo lockMinFreeStock(StockParam stockParam) {
        //1.查询出指定发型师,指定店铺最近可约的库存记录
        List<TStaffStock> tStaffStocks = staffStockMapper.selectMinFreeStockByStoreId(stockParam.getStoreId(), stockParam.getStaffId(), TimeUtil.DateToFormat(new Date(), "yyyy-MM-dd"));
        List<TStaffStock> filterStaffStocks = new ArrayList<>();
        if(CollectionUtils.isEmpty(tStaffStocks)){
            return null;
        }
        Date date = new Date();
        for(int i = 0; i < tStaffStocks.size(); i++){
            String stockDate = TimeUtil.DateToFormat(tStaffStocks.get(i).getDate(), "yyyy-MM-dd");
            String nowDate = TimeUtil.DateToFormat(date, "yyyy-MM-dd");
            if (stockDate.equals(nowDate)){
                filterStaffStocks.add(tStaffStocks.get(i));
            }
        }
        if(CollectionUtils.isEmpty(filterStaffStocks)){
            return null;
        }
        //2.拼接工作时间
        List<StockWorkTimeVo> workTimeVos = filterStockByWorkTime(filterStaffStocks);
        if(CollectionUtils.isEmpty(workTimeVos)){
            return null;
        }
        //3.排序
        Collections.sort(workTimeVos, Comparator.comparing(StockWorkTimeVo::getWorkTime));
        StockWorkTimeVo stockWorkTimeVo = workTimeVos.get(0);
        Long stockId = stockWorkTimeVo.getId();
        //4.锁定库存
        int i = staffStockMapper.updateStockBystockId(stockId, Long.parseLong(stockParam.getOrderNo()), stockParam.getStatus(), stockParam.getLineNo(), stockParam.getUserPath());
        if(i>0){
            TStaffStock staffStock = staffStockMapper.selectByPrimaryKey(stockId);
            rabbitMqSender.sendMessage("websocket_transfer_exchange", "websocket_transfer_key", JSON.toJSONString(new WebsocketMessageResponse<>(WORKPAD, String.valueOf(staffStock.getStoreId()), null)));
            StockForOrderVo stockForOrderVo = new StockForOrderVo(staffStock);
            return stockForOrderVo;
        }
        return null;

    }

    @Override
    public LineNumVo getLineNum(String dateTime, String staffId, String storeId) {
        LineNumVo lineNumVo = new LineNumVo();
        //计数器
        int count = 0;
        //准备数据 当前时间 date time 传入时间 date time 发型师id 店铺id
        String nowDateTime = TimeUtil.DateToFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
        String[] startDateTime = cutTime(nowDateTime);
        String[] endDateTime = cutTime(dateTime);
        List<TStaffStock> stocks = staffStockMapper.selectIntervalStocksByStaffIdAndStoreId(startDateTime[0], startDateTime[1], endDateTime[0], endDateTime[1], Long.parseLong(staffId), Long.parseLong(storeId));
        //1.先过滤掉当前时间之前的空闲库存
        for(int i =0; i < stocks.size(); i++){
            Date date = stocks.get(i).getDate();
            Date startTime = stocks.get(i).getStartTime();
            Date dateStock = TimeUtil.formatDate(TimeUtil.DateToFormat(date, "yyyy-MM-dd") + " " + TimeUtil.DateToFormat(startTime, "HH:mm:ss"));
            if(dateStock.before(new Date())){
                stocks.remove(stocks.get(i));
            }
        }
        //2.排队人数逻辑 使用处理过后的 库存集合
        for(TStaffStock staffStock : stocks){
            if(staffStock.getStatus() == 0){
                lineNumVo.setLineUp(false);
            }else{
                lineNumVo.setLineUp(true);
            }
        }
        if(lineNumVo.isLineUp()){
            for(TStaffStock staffStock : stocks){
                if(staffStock.getStatus() == 2){
                    count++;
                }
            }
            lineNumVo.setLineNo(count);
        }
        return lineNumVo;
    }

    /**
     * @description  下单屏库存排队逻辑列表
     * @Author 情绪极其不稳定的Shadow
     * @Param [storeId]
     * @return java.util.List<com.xingkeduo.model.vo.StockWorkTimeVo>
     **/
    @Override
    public OrderScreenVo getOrderScreenStock(String storeId) {
        OrderScreenVo orderScreenVo = new OrderScreenVo();
//        int count = 0;
        List<TStaffStock> staffStocks = staffStockMapper.selectAfterNowTimeStock(Long.parseLong(storeId), TimeUtil.DateToFormat(new Date(), "yyyy-MM-dd"));
        if(CollectionUtils.isEmpty(staffStocks)){
            return orderScreenVo;
        }

        //预处理时间格式
        List<StaffStockVo> voList = new ArrayList<>();
        for(TStaffStock staffStock : staffStocks){
            StaffStockVo staffStockVo = new StaffStockVo(staffStock);
            voList.add(staffStockVo);
        }
        List<StockWorkTimeVo> stockWorkTimeVos = getMinFreeStockByStoreId(Long.parseLong(storeId), null);
        if(CollectionUtils.isEmpty(stockWorkTimeVos)){
            return orderScreenVo;
        }
        for(StockWorkTimeVo stockWorkTimeVo : stockWorkTimeVos){
            List<StaffStockVo> newStocks = new ArrayList<>();
            for(int i = 0; i< voList.size(); i++){
                //根据发型师Id合并两个集合
                if(stockWorkTimeVo.getStaffId().equals(voList.get(i).getStaffId())){
//                    if(voList.get(i).getStatus() == 0){
//                        if(count > 0){
//                            continue;
//                        }
//                        count++;
//                    }
                    newStocks.add(voList.get(i));
                }
            }
            stockWorkTimeVo.setStaffStockList(newStocks);
//            count = 0;
        }

        //过滤集合 条件1-过滤掉当前之间节点之前 status 为 0 的数据 条件2 只保留当前时间节点最近的一个 status为0 的库存
        for(StockWorkTimeVo stockWorkTimeVo : stockWorkTimeVos){
            List<StaffStockVo> staffStockList = stockWorkTimeVo.getStaffStockList();
            List<StaffStockVo> filterStaffStocks = filterStockByWorkTimeAndStatus(staffStockList);
            stockWorkTimeVo.setStaffStockList(filterStaffStocks);
        }

        //拼接发型师数据
        List<Long> staffIds = new ArrayList<>();
        for(StockWorkTimeVo stockWorkTimeVo : stockWorkTimeVos){
            staffIds.add(stockWorkTimeVo.getStaffId());
            System.out.println(stockWorkTimeVo.getStaffId());
        }
        List<StaffInfoVo> staffInfoVos = otherService.getStaffInfoVo(staffIds);
        if(CollectionUtils.isEmpty(staffInfoVos)){
            return orderScreenVo;
        }
        for(StockWorkTimeVo stockWorkTimeVo : stockWorkTimeVos){
            for(StaffInfoVo staffInfoVo : staffInfoVos){
                if(stockWorkTimeVo.getStaffId().equals(staffInfoVo.getJobseekerId())){
                    if(!StringUtils.isEmpty(staffInfoVo.getCraftsName())){
                        stockWorkTimeVo.setCraftsName(staffInfoVo.getCraftsName());
                    }else if(!StringUtils.isEmpty(staffInfoVo.getName())){
                        stockWorkTimeVo.setCraftsName(staffInfoVo.getName());
                    }else{
                        stockWorkTimeVo.setCraftsName("******");
                    }
                    stockWorkTimeVo.setPath(staffInfoVo.getPath());
                }
            }
        }
        List<Long> staffIdList = new ArrayList<>();
        for(StockWorkTimeVo stockWorkTimeVo : stockWorkTimeVos){
            staffIdList.add(stockWorkTimeVo.getStaffId());
        }

        //价格处理
        //1.获取当前店铺下sku&价格
        List<ShopSkuInfoDto> shopsServprodList = otherService.getShopsServprodList(Long.parseLong(storeId));
        if(CollectionUtils.isEmpty(shopsServprodList) || shopsServprodList.size() == 0){
            orderScreenVo.setStockWorkTimeVoList(stockWorkTimeVos);
            return orderScreenVo;
        }
        //2.获取当前店铺下所有发型师的sku&价格
        ShopAndStaffListDto shopAndStaffListDto = otherService.getCraftsmanServiceList(storeId, staffIdList);
        //3.获取下单屏中间最大的价格（调用写好的接口http://gatewaydev.xingkeduo.com/api/op/shops/getStoreSkuInfoListByStoreId?storeId=69&sign=123）
        //TODO 此接口返回的属性 服务类型 属性名 serviceId
        //TODO getShopsServprodList接口返回的属性 服务类型 属性名 servprodId 不一致 导致有两个类似DTO 重构的时候注意
        List<ShopSkuAppInfoDto> skuAppInfoList = otherService.getStoreSkuInfoListByStoreId(Long.parseLong(storeId));
        //4.获取发型师对应服务类型
        List<StaffIdAndServiceIdDto> staffIdAndServiceIds = otherService.getStaffIdAndServiceIds(staffIdList);
        //5.获取下单屏最大的价格
        ScreenPriceDto screenPriceInfo = otherService.getScreenPriceInfo(Long.parseLong(storeId));

        //拼接下单屏价格
        //1.1获取店铺下单人剪发sku服务价格
        BigDecimal priceStoreSku = new BigDecimal("0");

        for(ShopSkuInfoDto shopSkuInfoDto : shopsServprodList){
            if(1L == shopSkuInfoDto.getServprodId()){
                priceStoreSku = shopSkuInfoDto.getPrice();
            }
        }
        List<StaffAndSkuListDto> staffAndSkuListDtos = shopAndStaffListDto.getServicePriceList();
        //最外层下单屏数据
        for(StockWorkTimeVo stockWorkTimeVo : stockWorkTimeVos){
            //发型师对应服务类型
            for(StaffIdAndServiceIdDto staffIdAndServiceIdDto : staffIdAndServiceIds){
                //找到同一个发型师
                if(stockWorkTimeVo.getStaffId().equals(staffIdAndServiceIdDto.getCraftsmanId())){
                    //判断该发型师是否有单人剪发服务
                    if(staffIdAndServiceIdDto.getServiceIds().contains(1L)){
                        //判断发型师是否配置了sku
                        if(CollectionUtils.isEmpty(staffAndSkuListDtos) || staffAndSkuListDtos.get(0).getServiceList() == null){
                            stockWorkTimeVo.setPrice(priceStoreSku);
                            break;
                        }
                        //店铺下所有发型师sku & 价格集合
                        for(StaffAndSkuListDto staffAndSkuListDto : staffAndSkuListDtos){
                            //对比发型师Id 相同处理价格逻辑
                            if(stockWorkTimeVo.getStaffId().equals(staffAndSkuListDto.getCraftsmanId())){
                                List<PriceAndServiceIdDto> serviceList = staffAndSkuListDto.getServiceList();
                                //判断发型师是否存在 单人剪发sku 存在是false 不存在是true TODO 未判断是否存在价格 是个坑
                                boolean present = serviceList.stream().filter(e -> e.getServiceId() == 1L).findAny().isPresent();
                                //存在单人剪发 Sku
                                if(present){
                                    //发型师所有sku & 价格 集合
                                    for(PriceAndServiceIdDto priceAndServiceIdDto : serviceList){
                                        if(priceAndServiceIdDto.getServiceId() == 1L){
                                            stockWorkTimeVo.setPrice(priceAndServiceIdDto.getPrice());
                                            break;
                                        }
                                    }
                                }else /** 发型师不存在单人剪发 Sku 那么直接取店铺单人剪发Sku**/{
                                    stockWorkTimeVo.setPrice(priceStoreSku);
                                }
                            }
                        }
                    }else/** 发型师不存单人剪发服务 则走取最低Sku价格逻辑**/{
                        System.out.println("发型师服务有："+ staffIdAndServiceIdDto.getServiceIds());
                        System.out.println("发型师不存单人剪发服务 则走取最低Sku价格逻辑");
                        //1.循环对比出发型师有哪些服务找出对应的sku&价格
                        List<PriceAndServiceIdDto> priceAndServiceIdDtos = new ArrayList<>();//第2步用到的对比集合
                        for(StaffAndSkuListDto staffAndSkuListDto : staffAndSkuListDtos){
                            if(stockWorkTimeVo.getStaffId().equals(staffAndSkuListDto.getCraftsmanId())){
                                List<PriceAndServiceIdDto> serviceList = staffAndSkuListDto.getServiceList();
                                //创建对比包含发型师sku价格集合
                                if(CollectionUtils.isEmpty(serviceList)) {
                                    for(ShopSkuInfoDto shopSkuInfoDto : shopsServprodList){
                                        PriceAndServiceIdDto serviceIdDto = new PriceAndServiceIdDto();
                                        serviceIdDto.setCraftsmanId(stockWorkTimeVo.getStaffId());
                                        serviceIdDto.setServiceId(shopSkuInfoDto.getServprodId());
                                        serviceIdDto.setPrice(new BigDecimal("0"));
                                        priceAndServiceIdDtos.add(serviceIdDto);
                                    }
                                    continue;
                                }
                                Map<Long,BigDecimal> serviceIdAndPrice = new HashMap<>();
                                for(PriceAndServiceIdDto priceAndServiceIdDto : serviceList){
                                    serviceIdAndPrice.put(priceAndServiceIdDto.getServiceId(), priceAndServiceIdDto.getPrice());
                                }
                                //获取发型师服务集合
                                List<Long> serviceIds = staffIdAndServiceIdDto.getServiceIds();
                                for(int i = 0;i<serviceIds.size();i++){
                                    BigDecimal decimal = serviceIdAndPrice.get(serviceIds.get(i));
                                    //判断发型师服务 是否存在调价sku如果不存在 那么价格为0 如果存在则价格为对应sku价格
                                    if (null == decimal){
                                        PriceAndServiceIdDto serviceIdDto = new PriceAndServiceIdDto();
                                        serviceIdDto.setCraftsmanId(stockWorkTimeVo.getStaffId());
                                        serviceIdDto.setServiceId(serviceIds.get(i));
                                        serviceIdDto.setPrice(new BigDecimal("0"));
                                        priceAndServiceIdDtos.add(serviceIdDto);
                                    }else {
                                        PriceAndServiceIdDto serviceIdDto = new PriceAndServiceIdDto();
                                        serviceIdDto.setCraftsmanId(stockWorkTimeVo.getStaffId());
                                        serviceIdDto.setServiceId(serviceIds.get(i));
                                        serviceIdDto.setPrice(serviceIdAndPrice.get(serviceIds.get(i)));
                                        priceAndServiceIdDtos.add(serviceIdDto);
                                    }
                                }
                            }
                        }
                        //2.循环对比店铺sku and 价格 & 第一步中结算的发型师sku and 价格 如有同服务发型师有sku价格 取发型师sku价格 无则取店铺sku价格
                        List<BigDecimal> decimalList = new ArrayList<>();
                        for(PriceAndServiceIdDto priceAndServiceIdDto : priceAndServiceIdDtos){
                            for(ShopSkuInfoDto shopSkuInfoDto : shopsServprodList){
                                if(priceAndServiceIdDto.getServiceId().equals(shopSkuInfoDto.getServprodId())){
                                    if(priceAndServiceIdDto.getPrice() == null || priceAndServiceIdDto.getPrice().compareTo(BigDecimal.ZERO) == 0){
                                        decimalList.add(shopSkuInfoDto.getPrice());
                                    }else{
                                        decimalList.add(priceAndServiceIdDto.getPrice());
                                    }
                                }
                            }
                        }
                        //3.找出2结果集中最小sku价格
                        BigDecimal min = Collections.min(decimalList);
                        stockWorkTimeVo.setPrice(min);
                    }
                }
            }
        }

        //设置下单屏主屏幕价格 TODO 一期需求
        /*for(ShopSkuAppInfoDto shopSkuAppInfoDto : skuAppInfoList){
            System.out.println(shopSkuAppInfoDto.getServiceId());
            if(1 == shopSkuAppInfoDto.getServiceId()){
                orderScreenVo.setPrice(shopSkuAppInfoDto.getPrice().divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));
            }
        }
        */

        //设置下单屏主屏幕价格 TODO 后加需求因为解释起来很麻烦，所以改成手动维护的了
        if(screenPriceInfo != null){
            if(!StringUtils.isEmpty(screenPriceInfo.getDisplayPrice())){
                BigDecimal bd =new BigDecimal(screenPriceInfo.getDisplayPrice());
                bd.setScale(2, BigDecimal.ROUND_HALF_UP);
                orderScreenVo.setPrice(bd);
            }else{
                orderScreenVo.setPrice(null);
            }
        }else {
            orderScreenVo.setPrice(null);
        }

        //设置下单屏主屏幕最早可约时间
        List<Integer> dates = new ArrayList<>();
        for(StockWorkTimeVo stockWorkTimeVo : stockWorkTimeVos){
            if(stockWorkTimeVo.getNearlyAppointmentTimestamp() == null || stockWorkTimeVo.getNearlyAppointmentTimestamp() == 0){
                continue;
            }
            dates.add(stockWorkTimeVo.getNearlyAppointmentTimestamp());
            System.out.println(TimeUtil.timestampToDate(stockWorkTimeVo.getNearlyAppointmentTimestamp().longValue()).substring(11,16));
        }

        //过滤掉不存在可约服务的发型师
        for(int i =0;i<stockWorkTimeVos.size();i++){
            System.out.println(stockWorkTimeVos.get(i).getStaffId()+"----"+stockWorkTimeVos.get(i).getCraftsName());
            if(stockWorkTimeVos.get(i).getStaffStockList().size() == 0 &&
                    stockWorkTimeVos.get(i).getNearlyAppointmentDateFormat().equals(TimeUtil.DateToFormat(new Date(), "yyyy-MM-dd"))){
                stockWorkTimeVos.remove(i);
            }
        }

        //TODO 还未处理发型师优惠内容 2019-06-04  2019-06-14已处理
        //查询发型师优惠情况
        Map<String, String> hairdresserActivitySummaryInfo = otherService.getHairdresserActivitySummaryInfo(Long.parseLong(storeId), staffIdList);
        //配置发型师优惠信息
        for(StockWorkTimeVo stockWorkTimeVo : stockWorkTimeVos){
            String flag = hairdresserActivitySummaryInfo.get(String.valueOf(stockWorkTimeVo.getStaffId()));
            if("true".equals(flag)){
                stockWorkTimeVo.setCoupon(true);
            }else{
                stockWorkTimeVo.setCoupon(false);
            }
        }

        if(!CollectionUtils.isEmpty(dates)){
            String date = TimeUtil.timestampToDate(Collections.min(dates).longValue());
            System.out.println("date--->>>"+date);
            log.info("下单屏最早可约时间date--->>>"+date);
            String today = TimeUtil.isToday(date, "yyyy-MM-dd");
            System.out.println(today);
            log.info("下单屏最早可约时间处理后today--->>>"+today);
            if("今天".equals(today)){
                orderScreenVo.setMinTime(TimeUtil.timestampToDate(Collections.min(dates).longValue()).substring(11,16));
            }else{
                orderScreenVo.setMinTime(today);
            }
        }else {
            orderScreenVo.setMinTime("");
        }

        for(StockWorkTimeVo stockWorkTimeVo : stockWorkTimeVos){
            List<StaffStockVo> staffStockList = stockWorkTimeVo.getStaffStockList();
            String nowDate = TimeUtil.DateToFormat(new Date(), "MM-dd");
            Iterator<StaffStockVo> iterator = staffStockList.iterator();
            while (iterator.hasNext()) {
                StaffStockVo next = iterator.next();
                log.info("库存时间date--->>>"+next.getDate());
                if (!nowDate.equals(next.getDate())) {
                    iterator.remove();
                }
            }

            if(CollectionUtils.isEmpty(staffStockList)){
                for(int i = 0; i<staffStockList.size(); i++){
                    log.info("第二边遍历库存时间date--->>>"+staffStockList.get(i).getDate());
                }
            }
            stockWorkTimeVo.setStaffStockList(staffStockList);
        }

        orderScreenVo.setStockWorkTimeVoList(stockWorkTimeVos);
        return orderScreenVo;
    }

    @Override
    public void receiveStaffId(Long staffId) {
        List<Long> storeIds = staffStockMapper.selectStoreIdByStaffId(staffId, TimeUtil.DateToFormat(new Date(), "yyyy-MM-dd"));
        for(int i=0;i<storeIds.size();i++){
            rabbitMqSender.sendMessage("websocket_transfer_exchange", "websocket_transfer_key", JSON.toJSONString(new WebsocketMessageResponse<>(WORKPAD, String.valueOf(storeIds.get(i)), null)));
        }
    }


    @Override
    public Map<String, StockVo> getStaffStock(Long staffId, Long storeId, String date) {
        SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
        Date starDate = null;
        if(StringUtils.isEmpty(date)){
            starDate = new Date();
        }else {
            starDate = TimeUtil.formatDate(date);
        }
        //生成需要查询的日期列表
        List<Date> dateList=TimeUtil.getAfterFiveDayByFirstDay(starDate);
        String endDate = TimeUtil.getFetureDate(4, starDate);
        //查询出来排班总量 TODO 此处有坑 如果当周没有数据  第二周 第三周有数据 不会返回 pageNum
        //准备数据
        int pageNum = 0;
        int attendanceDay = attendanceService.staffStoreMaxNumberOfDays(storeId,staffId);
        int num = attendanceDay%5;
        if(num == 0){
            pageNum = attendanceDay/5;
        }else{
            pageNum = attendanceDay/5+1;
        }
        List<TStaffStock> staffStocks = staffStockMapper.selectStockByStoreIDAndStaffIdAndDate(storeId,staffId,dateList);
        Map<String, StockVo> map = new LinkedHashMap<>();
        List<StaffStockVo> voList = new ArrayList<>();
        SimpleDateFormat formatMonthDay = new SimpleDateFormat("MM-dd");
        List<String> attendanceDates = new ArrayList<>();
        for(Date d:dateList){
            attendanceDates.add(formatMonthDay.format(d));
        }


        for(String attendanceDate : attendanceDates){
            map.put(attendanceDate, new StockVo(attendanceDate, TimeUtil.getWeekTime(attendanceDate), new ArrayList<>(), pageNum));
        }

        if (!CollectionUtils.isEmpty(staffStocks)) {
            //转化 Po--->Vo
            for (TStaffStock staffStock : staffStocks) {
                StaffStockVo vo = new StaffStockVo(staffStock);
                voList.add(vo);
            }
            //设置过时状态 懒过期
            for(StaffStockVo staffStockVo : voList){
                Date dateStock = TimeUtil.joinDateAndTime(staffStockVo.getDate(), staffStockVo.getStartTime());
                //判断时间是否过期 置灰
                if(dateStock.before(new Date())){
                    staffStockVo.setDisable(true);
                    //判断状态是否为空闲 标记过时
                    if(0 == staffStockVo.getStatus()){
                        staffStockVo.setOutOfDate(true);
                    }
                }
            }

            //准备数据
            for (StaffStockVo staffStockVo : voList) {
                if (map.containsKey(staffStockVo.getDate())) {
                    map.get(staffStockVo.getDate()).getStockList().add(staffStockVo);
                }
//                else {
//                    map.put(staffStockVo.getDate(), new StockVo(staffStockVo.getDate(), TimeUtil.getWeekTime(staffStockVo.getDate()), new ArrayList<>(), pageNum));
//                    map.get(staffStockVo.getDate()).getStockList().add(staffStockVo);
//                }
            }
        }
        return map;
    }



//    private List<TStaffStock> getAlreadyUsedStock(List<TStaffStock> sourceStaffStockList) {
//        List<TStaffStock> staffStockList = new ArrayList<>();
//        Iterator iterator = sourceStaffStockList.iterator();
//        while (iterator.hasNext()) {
//            TStaffStock staffStock = (TStaffStock) iterator.next();
//            //此处添加需要转移的库存状态（当前平移代付款，带服务状态库存）
//            if (staffStock.getStatus().equals(StockStatusEnum.WAIT_FOR_PAYMENT.getValue())
//                    || staffStock.getStatus().equals(StockStatusEnum.WAIT_FOR_SERVICE.getValue())) {
//                staffStockList.add(staffStock);
//            }
//        }
//        return staffStockList;
//    }

    private static long abs(long a) {
        return (a < 0) ? -a : a;
    }

    /**
     * @description  过滤集合中当前时间之前的库存
     * @Author 情绪极其不稳定的Shadow
     * @Param [tStaffStocks]
     * @return java.util.List<com.xingkeduo.model.vo.StockWorkTimeVo>
     **/
    private List<StockWorkTimeVo> filterStockByWorkTime(List<TStaffStock> tStaffStocks){
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm:ss");
        List<StockWorkTimeVo> workTimeVos = new ArrayList<>();
        for(TStaffStock tStaffStock: tStaffStocks){
            String workTime = formatDate.format(tStaffStock.getDate()) +" "+formatTime.format(tStaffStock.getStartTime());
            Date date = TimeUtil.stringToFormatDate(workTime);
            //TODO 需要加个判断 workTime 是否在当前时间之前 2019-05-14 已经处理
            if(!date.before(new Date())){
                StockWorkTimeVo workTimeVo = new StockWorkTimeVo(tStaffStock, workTime);
                workTimeVos.add(workTimeVo);
            }
        }
        return workTimeVos;
    }

    private String[] cutTime(String dateTime){
        String[] dateAndTime = dateTime.split(" ");
        return dateAndTime;
    }

    /**
     * @description  下单屏用来过滤库存 生成下单屏需求的库存队列
     * @Author 情绪极其不稳定的Shadow
     * @Param [tStaffStocks]
     * @return java.util.List<com.xingkeduo.model.po.TStaffStock>
     **/
    private List<StaffStockVo> filterStockByWorkTimeAndStatus(List<StaffStockVo> tStaffStocks){
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<StaffStockVo> timeStaffStocks = new ArrayList<>();
        for(StaffStockVo tStaffStock: tStaffStocks){
            if(tStaffStock.getStatus() == 2){
                timeStaffStocks.add(tStaffStock);
                continue;
            }
            String workTime = formatDate.format(TimeUtil.joinDateAndTime(tStaffStock.getDate(), tStaffStock.getStartTime()));
            Date date = TimeUtil.stringToFormatDate(workTime);
            if(!date.before(new Date()) && tStaffStock.getStatus() == 0){
                timeStaffStocks.add(tStaffStock);
            }
        }
        List<StaffStockVo> orderScreenStocks = new ArrayList<>();
        int count = 0;
        for(StaffStockVo staffStock : timeStaffStocks){
            if(staffStock.getStatus() == 0 && count == 0){
                orderScreenStocks.add(staffStock);
                count++;
            }else if(staffStock.getStatus()==2){
                orderScreenStocks.add(staffStock);
            }
        }
        return orderScreenStocks;
    }

    /**
     * CMS查看发型师库存
     * @param storeId
     * @return
     */
    @Override
    public CmsShopInfoVo getShopStockInfo(String date, Long storeId) {

        //查询店铺相关信息
        CmsShopsInfoDTO shopInfo = otherService.getShopInfo(storeId);
        CmsShopInfoVo cmsShopInfoVo = new CmsShopInfoVo();
        if(shopInfo != null){
            BeanUtils.copyProperties(shopInfo,cmsShopInfoVo);
            //查询店铺下经理和总经理的信息
            CmsCraftsmanShopDTO managerList = otherService.getManagerList(storeId, shopInfo.getOfficeId());
            if(managerList != null){
                cmsShopInfoVo.setCmsCraftsmanDTO(managerList.getCraftsman());
                cmsShopInfoVo.setCraftsmanList(managerList.getCraftsmanList());
            }
        }
        //查询店铺相关活动
        List<CmsActivityResponseDTO> shopActivityList = otherService.getShopActivityList(storeId);
        if(shopActivityList != null && shopActivityList.size()>0){
            cmsShopInfoVo.setCmsActivityResponseDTOList(shopActivityList);
        }

        List<TStaffStock> tStaffStocks = staffStockMapper.selectEnableStockByDateAndStoreId(TimeUtil.formatDate(date), storeId);

        //查询今日剩余可约库存数
        //当前日期和查询日期比较，大于直接返回0
        int compareDataResult = TimeUtil.compareData(date);
        int RemainingReducibleInventoryTodayCount = 0;
        String startDate = TimeUtil.DateToFormat(new Date(), "HH:mm");
        if(compareDataResult<0){
            startDate = "00:00";
        }
        if(compareDataResult>0){
            RemainingReducibleInventoryTodayCount = 0;
        }else{
            RemainingReducibleInventoryTodayCount = staffStockMapper.selectRemainingReducibleInventoryToday(storeId, TimeUtil.formatDate(date), startDate);
        }

        cmsShopInfoVo.setStockCount(Long.valueOf(RemainingReducibleInventoryTodayCount > 0 ? RemainingReducibleInventoryTodayCount: 0));

        //查询最早可约库存信息
        TStaffStock EarliestReducibleTimeInfo = staffStockMapper.selectEarliestReducibleTime(storeId, TimeUtil.formatDate(date), TimeUtil.DateToFormat(new Date(), "HH:mm"));
        if(EarliestReducibleTimeInfo != null){
            cmsShopInfoVo.setEarliestReducibleTime(TimeUtil.DateToFormat(EarliestReducibleTimeInfo.getStartTime(),"HH:mm"));
        }
        //排版查询
        List<CmsStaffInfoDTO> cmsStaffInfoDTOList = new ArrayList<>();
        boolean present;
        if(tStaffStocks!=null && tStaffStocks.size()>0){
            //这里计算一下库存颗粒
            Long datePoor = TimeUtil.getDatePoor(tStaffStocks.get(0).getEndTime(), tStaffStocks.get(0).getStartTime());
            //循环
            TStaffStockExtend tStaffStockExtend = staffStockMapper.selectEnableStockByDateAndStoreIdMaxAndMix(TimeUtil.formatDate(date), storeId);
            for (TStaffStock tStaffStock : tStaffStocks) {

                present = cmsStaffInfoDTOList.stream().filter(e -> e.getJobseekerId().equals(tStaffStock.getStaffId())).findAny().isPresent();
                if(!present){
                    CmsStaffInfoDTO cmsStaffInfoDTO = new CmsStaffInfoDTO();
                    cmsStaffInfoDTO.setJobseekerId(tStaffStock.getStaffId());

                    //获取发型师真实姓名和艺名（列表）
                    List<Long> staffIds = new ArrayList<>();
                    staffIds.add(tStaffStock.getStaffId());
                    Set setStaffId = new HashSet(staffIds);
                    List staffIdList = new ArrayList(setStaffId);
                    List<StaffInfoVo> staffInfoVoList = otherService.getStaffInfoVo(staffIdList);
                    if(staffInfoVoList != null && staffInfoVoList.size()>0){
                        for(StaffInfoVo infoVo : staffInfoVoList){
                            if(infoVo.getJobseekerId().equals(cmsStaffInfoDTO.getJobseekerId())){
                                cmsStaffInfoDTO.setName(infoVo.getName());
                                cmsStaffInfoDTO.setCraftsName(infoVo.getCraftsName());
                            }
                        }
                    }
                    cmsStaffInfoDTOList.add(cmsStaffInfoDTO);
                }
            }

            if(cmsStaffInfoDTOList!=null && cmsStaffInfoDTOList.size()>0){
                cmsShopInfoVo.setStaffCount(Long.valueOf(cmsStaffInfoDTOList.size()));
                //获取库存时间赋值
                List<String> timeInterval = TimeUtil.getTimeInterval(tStaffStockExtend.getStaffTimeMin(), tStaffStockExtend.getStaffTimeMax(), Math.toIntExact(datePoor));
                for (CmsStaffInfoDTO cmsStaffInfoDTO : cmsStaffInfoDTOList) {

                    List<CmsStaffStockVo> newCmsStaffInfoDTOList = new ArrayList<>();
                    //库存时间
                    List<CmsStaffStockVo> CmsStaffInfoDTOList = new ArrayList<>();
                    for (String s : timeInterval) {
                        CmsStaffStockVo cmsStaffStockVo = new CmsStaffStockVo();
                        cmsStaffStockVo.setStartTime(s);
                        cmsStaffStockVo.setStatus((byte) 10);
                        cmsStaffStockVo.setStatusName("无排班");
                        CmsStaffInfoDTOList.add(cmsStaffStockVo);
                    }
                    for (CmsStaffStockVo cmsStaffStockVo : CmsStaffInfoDTOList) {
                        for (TStaffStock tStaffStock : tStaffStocks) {
                            if (cmsStaffStockVo.getStartTime().equals(TimeUtil.DateToFormat(tStaffStock.getStartTime(), "HH:mm"))
                                    && tStaffStock.getStaffId().equals(cmsStaffInfoDTO.getJobseekerId())) {
                                cmsStaffStockVo.setId(tStaffStock.getId());
                                cmsStaffStockVo.setStatus(tStaffStock.getStatus());
                                cmsStaffStockVo.setStartTime(TimeUtil.DateToFormat(tStaffStock.getStartTime(), "HH:mm"));
                                cmsStaffStockVo.setStatusName(StockStatusEnum.getAttendanceType(tStaffStock.getStatus()).getDescription());
                                cmsStaffStockVo.setLineNo(tStaffStock.getLineNo());
                                continue;
                            }
                        }
                        newCmsStaffInfoDTOList.add(cmsStaffStockVo);
                    }
                    cmsStaffInfoDTO.setStaffName(newCmsStaffInfoDTOList);
                }
            }

            }
        cmsShopInfoVo.setStaffId(cmsStaffInfoDTOList);

        return cmsShopInfoVo;

    }



    /**
     * API查看发型师库存
     * @param storeId
     * @return
     */
    @Override
    public ApiShopInfoVo getApiShopStockInfo(String date, Long storeId) {

        //查询店铺相关信息
        CmsShopsInfoDTO shopInfo = otherService.getShopInfo(storeId);
        ApiShopInfoVo apiShopInfoVo = new ApiShopInfoVo();
        if(shopInfo != null){
            BeanUtils.copyProperties(shopInfo,apiShopInfoVo);
        }
        List<TStaffStock> tStaffStocks = staffStockMapper.selectEnableStockByDateAndStoreId(TimeUtil.formatDate(date), storeId);

        //查询今日剩余可约库存数
        //当前日期和查询日期比较，大于直接返回0
        int compareDataResult = TimeUtil.compareData(date);
        int RemainingReducibleInventoryTodayCount;
        String startDate = TimeUtil.DateToFormat(new Date(), "HH:mm");
        if(compareDataResult<0){
            startDate = "00:00";
        }
        if(compareDataResult>0){
            RemainingReducibleInventoryTodayCount = 0;
        }else{
            RemainingReducibleInventoryTodayCount = staffStockMapper.selectRemainingReducibleInventoryToday(storeId, TimeUtil.formatDate(date), startDate);
        }
        apiShopInfoVo.setStockCount(Long.valueOf(RemainingReducibleInventoryTodayCount > 0 ? RemainingReducibleInventoryTodayCount: 0));

        //排版查询
        List<CmsStaffInfoDTO> cmsStaffInfoDTOList = new ArrayList<>();
        boolean present;
        if(tStaffStocks!=null && tStaffStocks.size()>0){
            //这里计算一下库存颗粒
            Long datePoor = TimeUtil.getDatePoor(tStaffStocks.get(0).getEndTime(), tStaffStocks.get(0).getStartTime());
            //循环
            TStaffStockExtend tStaffStockExtend = staffStockMapper.selectEnableStockByDateAndStoreIdMaxAndMix(TimeUtil.formatDate(date), storeId);
            for (TStaffStock tStaffStock : tStaffStocks) {

                present = cmsStaffInfoDTOList.stream().filter(e -> e.getJobseekerId().equals(tStaffStock.getStaffId())).findAny().isPresent();
                if(!present){
                    CmsStaffInfoDTO cmsStaffInfoDTO = new CmsStaffInfoDTO();
                    cmsStaffInfoDTO.setJobseekerId(tStaffStock.getStaffId());
                    //获取发型师真实姓名和艺名（列表）
                    List<Long> staffIds = new ArrayList<>();
                    staffIds.add(tStaffStock.getStaffId());
                    Set setStaffId = new HashSet(staffIds);
                    List staffIdList = new ArrayList(setStaffId);
                    List<StaffInfoVo> staffInfoVoList = otherService.getStaffInfoVo(staffIdList);
                    if(staffInfoVoList != null && staffInfoVoList.size()>0){
                        for(StaffInfoVo infoVo : staffInfoVoList){
                            if(infoVo.getJobseekerId().equals(cmsStaffInfoDTO.getJobseekerId())){
                                cmsStaffInfoDTO.setName(infoVo.getName());
                                cmsStaffInfoDTO.setCraftsName(infoVo.getCraftsName());
                            }
                        }
                    }
                    cmsStaffInfoDTOList.add(cmsStaffInfoDTO);
                }
            }

            if(cmsStaffInfoDTOList!=null && cmsStaffInfoDTOList.size()>0){
                apiShopInfoVo.setStaffCount(Long.valueOf(cmsStaffInfoDTOList.size()));
                //获取库存时间赋值
                List<String> timeInterval = TimeUtil.getTimeInterval(tStaffStockExtend.getStaffTimeMin(), tStaffStockExtend.getStaffTimeMax(), Math.toIntExact(datePoor));
                for (CmsStaffInfoDTO cmsStaffInfoDTO : cmsStaffInfoDTOList) {

                    List<CmsStaffStockVo> newCmsStaffInfoDTOList = new ArrayList<>();
                    //库存时间
                    List<CmsStaffStockVo> CmsStaffInfoDTOList = new ArrayList<>();
                    for (String s : timeInterval) {
                        CmsStaffStockVo cmsStaffStockVo = new CmsStaffStockVo();
                        cmsStaffStockVo.setStartTime(s);
                        cmsStaffStockVo.setStatus((byte) 10);
                        cmsStaffStockVo.setStatusName("无排班");
                        CmsStaffInfoDTOList.add(cmsStaffStockVo);
                    }
                    for (CmsStaffStockVo cmsStaffStockVo : CmsStaffInfoDTOList) {
                        for (TStaffStock tStaffStock : tStaffStocks) {
                            if (cmsStaffStockVo.getStartTime().equals(TimeUtil.DateToFormat(tStaffStock.getStartTime(), "HH:mm"))
                                    && tStaffStock.getStaffId().equals(cmsStaffInfoDTO.getJobseekerId())) {
                                cmsStaffStockVo.setId(tStaffStock.getId());
                                cmsStaffStockVo.setStatus(tStaffStock.getStatus());
                                cmsStaffStockVo.setStartTime(TimeUtil.DateToFormat(tStaffStock.getStartTime(), "HH:mm"));
                                cmsStaffStockVo.setStatusName(StockStatusEnum.getAttendanceType(tStaffStock.getStatus()).getDescription());
                                cmsStaffStockVo.setLineNo(tStaffStock.getLineNo());
                                cmsStaffStockVo.setOrderNo(String.valueOf(tStaffStock.getOrderId()));
                                continue;
                            }
                        }
                        newCmsStaffInfoDTOList.add(cmsStaffStockVo);
                    }
                    cmsStaffInfoDTO.setStaffName(newCmsStaffInfoDTOList);
                }
            }

        }
        apiShopInfoVo.setStaffId(cmsStaffInfoDTOList);

        return apiShopInfoVo;
    }











    @Override
    public Boolean getStockIsEnable(Long storeId, Long orderNo) {
        TStaffStock staffStock = staffStockMapper.selectByStoreIdAndOrderNo(storeId, orderNo);
        if(null == staffStock){
            return false;
        }
        return true;
    }

}
